package com.pe.adexus.portal3g.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.pe.adexus.portal3g.componente.Browser;
import com.pe.adexus.portal3g.componente.Constantes;

public class PrimeraVezServlet extends HttpServlet {
	private String clase="PrimeraVezServlet";
	private String path =null;
	
	static final Logger log = Logger.getLogger(LoginServlet.class);// objeto que imprime en el log de errores
	
	/**
	 * Constructor of the object.
	 */
	public PrimeraVezServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.procesoServlet(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.procesoServlet(request,response);
	}

	
	/**
	 * Metodo que realiza las operaciones respectivas del servlet
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public String procesoServlet(HttpServletRequest request, HttpServletResponse response)
										throws ServletException, IOException {
		/*Date fecha = new Date();
		System.out.println("fecha hora= "+fecha.getHours()+" minuto="+fecha.getMinutes());
		*/
		String metodo="procesoServlet";
		HttpSession session = request.getSession(true);
		String IP=null;
		try{
			/**
			 * Definiendo la ruta a mostrar en la variable path
			 * Si fuera movil, deriva a la ruta mobile.
			 */
			if(Browser.isMobile(request.getHeader("User-Agent")))
				path="/mobile";
			else
				path="";
			/**
			 */		
			
			
			String first_access = "true";
			session.setAttribute("first_access", first_access);
			String page = request.getParameter("page");
			session.setAttribute("page", page);
			IP  =request.getRemoteAddr();
			session.setAttribute("action", Constantes.INICIO_PORTAL);
			response.sendRedirect(request.getContextPath()+"/LoginServlet");
			return "";
		}catch(Exception e){
			log.error("<Ocurrio un excepcion");
			log.error(Constantes.PORTAL+"; Clase: "+this.clase+" , Metodo: "+metodo);
			log.error("Mensaje : "+e+">");
			e.printStackTrace();
			request.getRequestDispatcher(this.path+"/page/error.jsp").forward(request,response);
		}finally{
		}
		return "";
		
	}
	
	
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
